import React, { useEffect } from 'react'
import { useHistory } from 'react-router-dom'
import { Switch, Route } from 'react-router-dom'
import routes from './routes'

const Routes = () => {
  const history = useHistory()
  const token = localStorage.getItem('token')

  useEffect(() => {
    if (!token) {
      history.push('/login')
    }
  }, [history, token])

  return (
    <Switch>
      {
        routes.map(v => {
          return (
            <Route
              exact={v.path === '/'}
              key={v.path}
              path={v.path}
              component={v.component}
            />
          )
        })
      }
    </Switch>
  )
}

export default Routes